package edu.usc.tweetool.logic;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Date;

import twitter4j.IDs;
import twitter4j.Status;
import twitter4j.Twitter;
import twitter4j.TwitterException;
import twitter4j.TwitterFactory;
import twitter4j.User;
import twitter4j.auth.AccessToken;
import twitter4j.auth.RequestToken;
import edu.usc.tweetool.hibernate.Follow;
import edu.usc.tweetool.hibernate.FollowDAO;
import edu.usc.tweetool.hibernate.FollowId;
import edu.usc.tweetool.hibernate.HighQueue;
import edu.usc.tweetool.hibernate.HighQueueDAO;
import edu.usc.tweetool.hibernate.HighQueueId;
import edu.usc.tweetool.hibernate.RegUser;
import edu.usc.tweetool.hibernate.RegUserDAO;
import edu.usc.tweetool.hibernate.UserThetaDAO;

public class UserProcess {

	private FollowDAO followDAO;
	private HighQueueDAO hqDAO;
	private RegUserDAO ruDAO;
	private UserThetaDAO utDAO;
	private Twitter twitter;

	public static final String CUSTOMER_KEY = "5ob63xUZA7MMHPWZURJ8w";
	public static final String CUSTOMER_SECRET = "BjH6JUZ03aymAu5Gs6y1gF24hiFRqfWGT8uHtzucc";
	public static String ACCESS_KEY = null;
	public static String ACCESS_SECRET = null;

	public UserProcess() {

	}

	public void processLogin() {
		try {
			String username = twitter.getScreenName();

			System.out.println("New User Process: Start");
			System.out.println("New User: " + twitter.getScreenName());

			hqDAO.save(new HighQueue(new HighQueueId(twitter.getScreenName(),
					new Date().getTime() / 1000)));

			// if (ruDAO.findById(userId) == null) {
			// ruDAO.save(new RegUser(userId));
			// }

			IDs idss = twitter.getFriendsIDs(-1);
			long[] ids = idss.getIDs();
			System.out.println("Ids: " + idss.toString() + " " + ids.length);
			for (long id : ids) {
				User tuser = twitter.showUser(id);
				if (followDAO.findById(new FollowId(username, tuser
						.getScreenName())) == null) {
					followDAO.save(new Follow(new FollowId(username, tuser
							.getScreenName())));
				}
				hqDAO.save(new HighQueue(new HighQueueId(tuser.getScreenName(),
						new Date().getTime() / 1000)));
			}
			System.out.println("New User Process: Success");

		} catch (IllegalStateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TwitterException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public void updateStatement(String statement) {
		String statusText = statement;
		try {
			Status status = twitter.updateStatus(statusText);
			System.out.println("Successfully updated the status to ["
					+ status.getText() + "].");
		} catch (TwitterException te) {
			te.printStackTrace();
			System.out.println("Failed to get timeline: " + te.getMessage());
		}
	}

	public FollowDAO getFollowDAO() {
		return followDAO;
	}

	public void setFollowDAO(FollowDAO followDAO) {
		this.followDAO = followDAO;
	}

	public HighQueueDAO getHqDAO() {
		return hqDAO;
	}

	public void setHqDAO(HighQueueDAO hqDAO) {
		this.hqDAO = hqDAO;
	}

	public RegUserDAO getRuDAO() {
		return ruDAO;
	}

	public void setRuDAO(RegUserDAO ruDAO) {
		this.ruDAO = ruDAO;
	}

	public UserThetaDAO getUtDAO() {
		return utDAO;
	}

	public void setUtDAO(UserThetaDAO utDAO) {
		this.utDAO = utDAO;
	}

	public void setTwitter(Twitter twitter) {
		this.twitter = twitter;
	}
}
